package com.ddy.test.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.Date;

/**
 * Created by ddy on 2017/12/6.
 *
 *  Job多线程中每个线程都是一个new的对象,所以只能将锁加在 类 上
 *
 *  使用synchronized(this){  } 无效
 *  使用synchronized void executeTask(){} 无效
 *
 */
public class JobDemo implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //executeTask(context);

        synchronized (JobDemo.class){
            //打印任务详情
            System.out.println(new Date() + " staring " +
                    context.getJobDetail().getGroup()
                    +"——"+context.getJobDetail().getName()
                    +"——"+context.getTrigger().getName()
                    +"——"+context.getTrigger().getGroup());
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(new Date() + " end " +
                    context.getJobDetail().getGroup()
                    +"——"+context.getJobDetail().getName()
                    +"——"+context.getTrigger().getName()
                    +"——"+context.getTrigger().getGroup());
        }
    }

    public synchronized void executeTask(JobExecutionContext context) throws JobExecutionException{
        //打印任务详情
        System.out.println(new Date() + " staring " +
                context.getJobDetail().getGroup()
                +"——"+context.getJobDetail().getName()
                +"——"+context.getTrigger().getName()
                +"——"+context.getTrigger().getGroup());
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(new Date() + " end " +
                context.getJobDetail().getGroup()
                +"——"+context.getJobDetail().getName()
                +"——"+context.getTrigger().getName()
                +"——"+context.getTrigger().getGroup());
    }


}